//
//  JRHttpRequestEncryptionManager.swift
//  JRHttpRequestManager
//
//  Created by 京睿 on 2017/8/8.
//  Copyright © 2017年 JingRuiWangKe. All rights reserved.
//

import Foundation
import JRString

/// 加密接口
public protocol JRHttpRequestEncryptionInterface {
    mutating func encryptionResult(_ origin: Any!) -> Any!
}

/// 排序MD5加密类族
public class JRMD5EncryptionManager: JRHttpRequestEncryptionInterface {
    public func encryptionResult(_ origin: Any!) -> Any! {
        func sortedPapameters(_ parameters: [String: Any]) -> String {
            let sortedResult = parameters.sorted(by: {$0.0 < $1.0})
            var arrayResult = [String]()
            sortedResult.forEach {
                let parameter = $0.key + "=" + String(describing: $0.value)
                arrayResult += [parameter]
            }
            let foo = arrayResult.joined(separator: "&")
            return foo + "Oo0l3б6З|1ιiì`'〃'v√κx×ɡвёВБъbыИn"
        }
        
        guard var foo = origin as? [String: AnyObject] else { return nil }
        
        let sign = sortedPapameters(foo)
        foo["sign"] = sign.JRMD5Bit32String() as AnyObject?
        
        return foo
    }
}
